<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class tool extends CI_Controller {
var $cookieFileName;
var $forum;
	function __construct() {
		
        parent::__construct();
		$this->load->model("mvideo");
		$this->load->model("mseries");
		$this->load->model("mprogram");
		$this->load->model("mcategory");
		//$this->load->model("getsite/tvzing");
		$this->load->library('get_data_html');
		$this->cookieFileName = dirname (realpath(__FILE__)).'/'.$this->cookieFileName; 
    }
    public function field($table_name = '') {
        $fields = $this->db->field_data($table_name);
        echo '$fieldData = array();';
        echo "<br>";
        foreach ($fields as $field) {
            if ($field->default == '')
                $data = $this->fieldDefault($field->name, $field->type);
            else
                $data = $field->default;
            echo '$fieldData[\'' . $field->name . '\'] = ' . $data . ';';
            echo "<br>";
        }
        echo 'return $fieldData;';
    }

    function fieldDefault($name, $type = '') {
        $default = "";

        //time
        if (strpos($name, "TIME_"))
            $default = 'time()';
        else if (strpos($name, "_STATUS"))
            $default = '1';
        else if ($type == 'bigint' || $type == 'int')
            $default = '0';
        else
            $default = "''";
        return $default;
    }
	function update_program(){
	$this->load->model("mprogram");
	$programs = $this->mprogram->gets();
		foreach($programs as $program){
			$arr_update = array();
			$arr_update['PROG_SULG'] = $this->seo->seolink($program->PROG_TITLE);
			$this->mprogram->update($program->PROG_ID,'PROG_ID',$arr_update);
		}
	}
	function update_video(){
	$this->load->model("mprogram");
		$this->load->model("mseries");
		$this->load->model("mvideo");
		$this->load->model("seo");
		set_time_limit(999999999999);
		echo '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />';
		$videos = $this->mvideo->gets();
		
		foreach($videos as $video){
			$arr_update = array();
			$arr_update['VID_TYPE'] = 'tv.zing.vn';
			$arr_update['VID_SLUG'] = $this->seo->seolink($video->VID_TITLE);
			$arr_update['VID_TIMECREATE'] = time();
			$arr_update['VID_TIMEUPDATE'] = time();
			
			$tags = get_meta_tags($video->VID_LINK,true);
			$arr_update['SEO_H1'] = $video->VID_TITLE;
			$arr_update['SEO_METATITLE'] = $video->VID_TITLE;
			$arr_update['SEO_KEYWORD'] = trim($tags['keywords']);
			$arr_update['SEO_DESCRIPTION'] = trim($tags['description']);
			$this->mvideo->update($video->VID_ID,'VID_ID',$arr_update);
		}
	
	}
	
	function update_series(){
		$this->load->model("mprogram");
		$this->load->model("mseries");
		$this->load->model("mvideo");
		$this->load->model("seo");
		set_time_limit(999999999999);
		echo '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />';
		$videos = $this->mseries->gets();
	
		foreach($videos as $video){
			$arr_update = array();
			$arr_update['SER_CODE'] = $this->seo->seolink($video->SER_TITLE);
			$this->mseries->update($video->SER_ID,'SER_ID',$arr_update);
		}
	
	}
	
	function clone_video_title() {
		$this->load->model("mvideo");
		$this->load->model("seo");
		set_time_limit(999999999999);
		
		$vs= $this->mvideo->gets(array("SEO_METATITLE "=>""));
		$a = array();
		if ($vs) foreach ($vs as $v) {
			//$a = (array) $v;
			$a['SEO_METATITLE'] = $v->VID_TITLE;
			$this->mvideo->update($v->VID_ID,'VID_ID', $a);
		}
	}
	
	function clone_program_title() {
		$this->load->model("mprogram");
		$this->load->model("seo");
		set_time_limit(999999999999);
		
		$vs= $this->mprogram->gets(array("SEO_METATITLE "=>""));
		$a = array();
		if ($vs) foreach ($vs as $v) {
			//$a = (array) $v;
			$a['SEO_METATITLE'] = $v->PROG_TITLE;
			$this->mprogram->update($v->PROG_ID,'PROG_ID', $a);
		}
	}
	
	function update_program_cover_bychanel($chanel) {
		if (!isset($_GET['thphuc'])) 
		die("File not found");
		$this->load->model("myoutube");
		$youtube = $this->myoutube->get($chanel, "YOUT_LINK");
		if(isset($youtube->MY_ID) && $youtube->MY_ID > 0 && $youtube->YOUT_TYPE=='user') {
			$cover = $_GET['u'];
			$this->load->model("mprogram");
		
			$this->mprogram->update($youtube->MY_ID, 'PROG_ID', array("PROG_COVER"=> $cover));
			echo "OK: " .$cover;
			
			
		}else {
			die ("Access die");
		}
		
	}
	
	function update_program_cover($code) {
		if (!isset($_GET['thphuc'])) 
		die("File not found");
		$cover = $_GET['u'];
		$this->load->model("mprogram");
		$v= $this->mprogram->gets($code, "PROG_CODE");
		
		if (isset($v[0]->PROG_ID)) {
			$this->mprogram->update($code, 'PROG_CODE', array("PROG_COVER"=> $cover));
			echo "OK: " .$cover;
		}
		else 
			echo "Error";
	}
	
	function build_link_cache(){
		$this->load->helper('file');
		$cates = $this->mcategory->gets();
		$data_temp =array();
		$data = "<?php \n";
        $data.="return ";
		if($cates) {
			foreach($cates as $cate){
			if($cate->CAT_CODE != '')
				$data_temp[] = strtolower($cate->CAT_CODE);
			}
		$data_temp = var_export($data_temp, true);
		$data.= $data_temp;
        $data.="?>\n";
        write_file('./mcache/category_router.php', $data);
		}
		
		$programs = $this->mprogram->gets();
		$data_temp =array();
		$data = "<?php \n";
        $data.="return ";
		if($programs) {
			foreach($programs as $program){
			if($program->PROG_CODE != '')
				$data_temp[] = strtolower($program->PROG_CODE);
			}
		$data_temp = var_export($data_temp, true);
		$data.= $data_temp;
        $data.="?>\n";
        write_file('./mcache/program_router.php', $data);
		}
	
	}
	function check_curl() {
    if  (in_array  ('curl', get_loaded_extensions())) {
         echo "cURL is <span style=\"color:blue\">installed</span> on this server";
    }
    else {
        echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
    }
	}
	
	function post_gif() {
		$url = $this->input->get('link');
		if (!$url) 
			die('-1');
		$info = pathinfo($url);
		if (!$info) die('-2');;
		$this->db->insert('gif', array(
			'gif_date' => time(),
			'gif_link' => $url,
			'gif_name' => $info['filename']
		));
	}
}
